<?php

namespace App\Console\Commands;

use App\Models\Report;
use Illuminate\Console\Command;
use Carbon\Carbon;

class ChargeCountYear extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'charge:year-report';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收费统计年报表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $res = Report::whereRaw("date_format(report_date,'%Y')='".date('Y',strtotime("-1 day"))."'")
            ->where('type','flow_count_month_report')->get();

        $values = $res->map(function ($item){
            return $item->value;
        });

        //对进行月累加
        $newArr=[];
        $row=$values->first();
        foreach ($row as $k=>$v){
            $newArr[$k]=$values->sum($k);
        }

        //存入数据库
        $report = new Report();
        $report->report_date = Carbon::yesterday()->year;
        $report->type = 'charge_count_year_report';
        $report->value = $newArr;
        $report->save();

        $this->info('年收费统计成功');
    }
}
